package com.cyw.common.config.jackson.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * 字符串首尾去除空格
 * @Author:cyw
 * @CreateTime: 2021年1月19日09:34:09
 **/
public class TrimStringDeserializer extends StdDeserializer<String> {
    private static final long serialVersionUID = -6279167254807737707L;

    public TrimStringDeserializer() {
        this(null);
    }

    public TrimStringDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String text = StringUtils.trim(p.getText());

        if (StringUtils.isEmpty(text) || "null".equals(text)) {
            return "";
        }

        return text;
    }
}
